<?php

namespace App\Manage;


use App\Models\User;
use App\Models\UserWithdraw;
use App\Service\Base\Manage;
use Illuminate\Support\Facades\Log;

class UserWithdrawManage extends Manage
{

    // 提现
    public function initialize()
    {
        parent::initialize();

        $this->model = 'UserWithdraw';
    }

    // 审核
    public function onVerify()
    {
        //   如果审核通过后直接提现到用户
        $opt    = $this->option();
        $_value = $opt->getData('value', null, 'intval');
        $_id    = $opt->getData('id', 0, 'intval');
        if(is_null($_value)){
            return $this->_returnFail('状态必须填写！！');
        }
        if(empty($_id)){
            return $this->_returnFail('请指定操作记录！！');
        }
        if(!in_array($_value, [1, 2])){
            return $this->_returnFail('审核状态必须为2或1！！');
        }

        // 查询订单信息
        $withDb = new UserWithdraw();
        $info   = $withDb->where('id', $_id)->first();
        if(empty($info)){
            return $this->_returnFail('提现记录不存在！！');
        }

        // 查询用户信息
        if($_value == 1){
            $uInfo = (new User())->where('id', $info['uid'])->first();
            if(empty($uInfo)){
                return $this->_returnFail('提现用户不存在或者已经被删除！！');
            }
            if(empty($uInfo['openid'])){
                return $this->_returnFail('提现用户还未绑定微信小程序，不能提现！！');
            }
        }

        // 操作成功
        $result = $withDb->where('id', $_id)->update(['verify' => $_value]);
        if($result === false){
            return $this->_returnFail($withDb->getError());
        }

        // 删除缓存
        $withDb->deleteCacheShow($info);

        // 执行提现
        if($_value == 1){
            //return $this->_returnFail('微信提现失败！！');


            // 执行提醒
            $payV3  = new MiniPayLogicV3();
            $result = $payV3->transfer($info['trade_no'], $info['money'], $uInfo['openid']);

            // 删除缓存
            $withDb->deleteCacheShow($info);
            if($result === false){
                // 取消审核
                $this->cancelVerify($_id);

                return $this->_returnFail($payV3->getError());
            }

            Log::info('提现:return'.json_encode($result, JSON_UNESCAPED_UNICODE));
            if(!empty($result['code']) && ($result['code'] != 200)){
                // 取消审核
                $this->cancelVerify($_id);

                return $this->_returnFail($result['message'] ?? '退款操作失败！');
            }
        }

        return $result;
    }

    // 取消提现审核
    protected function cancelVerify($_id)
    {
        return (new UserWithdraw())->where('id', $_id)->update(['verify' => 0]);
    }

    // 列表查询
    protected function listWhere()
    {
        $option   = $this->option();
        $where    = [];
        $bang     = [];
        $uid      = $option->getData('uid', 0, 'intval');
        $s_time   = $option->getData('start_time', 0, 'strtotime');
        $e_time   = $option->getData('end_time', 0, 'strtotime');
        $trade_no = $option->getData('trade_no', null, 'trim');
        $keyword  = $option->getData('keyword', null, 'trim');
        if($uid){
            $bang[]  = 'uid';
            $where[] = ['uid', '=', $uid];
        }
        if($s_time){
            $bang[]  = 'ctime';
            $where[] = ['ctime', '>=', $s_time];
        }
        if($e_time){
            $bang[]  = 'ctime';
            $where[] = ['ctime', '<=', $e_time];
        }
        if($trade_no){
            $bang    = false;
            $where[] = ['trade_no', 'like', "%".$trade_no."%"];
        }
        if($keyword){
            $bang = false;
            //$where[] = $this->orWhere('username|nickname|mobile', 'like', $keyword);
        }

        // 缓存
        $this->bang = $bang;

        return $where;
    }


    // 添加之前
    protected function _before_add()
    {
        $opt   = $this->option();
        $uid   = $opt->getData("uid", null, "intval");
        $money = $opt->getData("money", null, "trim");
        // 判断用户提现金额
        if(empty($uid)){
            return $this->_returnFail('请选择提现用户！');
        }
        if(empty($money)){
            return $this->_returnFail('请输入提现金额！');
        }

        // 判断用户金额
        $uInfo = (new User())->isCache(true)->oneFind(['id' => $uid]);
        if(empty($uInfo)){
            return $this->_returnFail('请提现用户不存在！');
        }
        if(empty($uInfo['openid'])){
            return $this->_returnFail('请提现用户未绑定微信！');
        }
        if($uInfo['money'] < $money){
            return $this->_returnFail('用户余额不足！');
        }

        $data = [
            //'id'          => $opt->getData("id", null, "intval"),
            'uid'         => $uid,
            'money'       => $money,
            'trade_no'    => $opt->getData("trade_no", null, "trim"),
            'description' => $opt->getData("description", null, "trim"),
            //'ctime'       => $opt->getData("ctime", null, "intval"),
            //'mtime'       => $opt->getData("mtime", null, "intval"),
        ];

        return $data;
    }

    // 修改之前
    protected function _before_edit()
    {
        $opt   = $this->option();
        $uid   = $opt->getData("uid", null, "intval");
        $money = $opt->getData("money", null, "trim");
        $id    = $this->id();
        // 判断用户提现金额
        if(empty($uid)){
            return $this->_returnFail('请选择提现用户！');
        }
        if(empty($money)){
            return $this->_returnFail('请输入提现金额！');
        }
        if(empty($id)){
            return $this->_returnFail('请指定修改记录！');
        }

        // 判断提现是否已经完成
        //  审核以后不能在修改，
        $info = (new UserWithdraw())->isCache(true)->oneFind(['id' => $id]);
        if(empty($info)){
            return $this->_returnFail('你要修改的记录不存在！');
        }
        if($info['verify'] == 3){
            return $this->_returnFail('提现成功了的，不能在修改！');
        }
        if($info['verify'] > 0){
            return $this->_returnFail('审核后的，不能在修改！');
        }

        // 判断用户金额
        $uInfo = (new User())->isCache(true)->oneFind(['id' => $uid]);
        if(empty($uInfo)){
            return $this->_returnFail('请提现用户不存在！');
        }
        if(empty($uInfo['openid'])){
            return $this->_returnFail('请提现用户未绑定微信！');
        }
        if($uInfo['money'] < $money){
            return $this->_returnFail('用户余额不足！');
        }


        $data = [
            //'id'          => $opt->getData("id", null, "intval"),
            'uid'         => $uid,
            'money'       => $money,
            'trade_no'    => $opt->getData("trade_no", null, "trim"),
            'description' => $opt->getData("description", null, "trim"),
            //'ctime'       => $opt->getData("ctime", null, "intval"),
            //'mtime'       => $opt->getData("mtime", null, "intval"),
        ];

        return $data;
    }

}


